<?php
// +----------------------------------------------------------------------
// | LuShop [ Lulu COMPANY WEB SHOW]
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://www.luluui.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: uuleaf <uuleaf@163.com>
// +----------------------------------------------------------------------
class OrderformAction extends MbaseAction {
	public $tbname = 'Orderform';
	public $obj = '';
	public $mpk = 'orderform_id';
	public function index() {
		$member_id = $_SESSION['member_id'];
		$morder = D('Orderform');
		$progress = isset ($_GET['progress']) ? trim($_GET['progress']) : 0;
		$map = array (
			'q' => array (
				'member_id' => $member_id,
				
			),
			'o' => array (
				'oby' => 'created',
				'okey' => 'desc'
			),
			'r' => true,
			'p' => 5,
			
		);
		if ($progress)
			$map['q']['progress'] = $progress;
		$datalist = $morder->getList($map);
		foreach ($datalist as $aorder) {
			$morder->autoDeal($aorder['orderform_id']);
		}
		$this->assign('datalist', $datalist);

		$this->assign('isindex', 1);
		$this->assign('isorder', 1);
		$this->display();
	}
	public function view() {
		$mpoints = D('Member_points');
		$bonus_rate = $mpoints->init_rate();
		$this->assign('bonus_rate', $bonus_rate['bonus_income_rate']);
		$this->assign('isindex', 1);
		$this->assign('isorder', 2);
		$this->cshow();
	}
	public function create() {
		$member_id =  isset($_SESSION['member_id'])? $_SESSION['member_id']:0;
		if (!$this->isPost()) {
			//购物车信息
			$cartinfo = cookie('mycart');
			$cartinfo = json_decode(stripcslashes($cartinfo),true);
			$goodlist = $cartinfo['good'];
			if(empty($goodlist)) $this->error('无商品记录');
			$allprice = 0;
			foreach($goodlist as $adata){
				$allprice += $adata['total'];
			}
			$freight_fee = $this->getConfig('freight_fee');
			$get_rate = $this->getConfig('getrate');
			$this->assign('freight_fee', $freight_fee);
			$this->assign('total_fee', $allprice);
			$final_fee = $allprice + $freight_fee;
			$this->assign('final_fee', $final_fee);
			$this->assign('datalist', $goodlist);
			//会员信息
			$mmember = D('Member');
			$memberinfo = $mmember->relation('content')->find($member_id);
			$this->assign('memberinfo', $memberinfo);
			$this->assign('isindex', 1);
			$this->assign('isorder', 3);
			$this->display();
		} else {
			$member_id = isset($_SESSION['member_id'])?$_SESSION['member_id']:0;
			import("ORG.Util.Input");
			//取得运费
			$freight_fee = $this->getConfig('freight_fee');
			//基本订单信息
			$datainfo['member_id'] = $member_id;
			$datainfo['code'] = $this->getOrderformCode();
			$datainfo['pay_type'] = isset ($_POST['pay_type']) ? Input :: getVar(trim($_POST['pay_type'])) : 1;
			$datainfo['express_type'] = '';
			$datainfo['express_code'] = '';
			$datainfo['consignee'] = isset ($_POST['consignee']) ? Input :: getVar(trim($_POST['consignee'])) : '';
			$datainfo['consignee_addr'] = isset ($_POST['consignee_addr']) ? Input :: getVar(trim($_POST['consignee_addr'])) : '';
			$datainfo['consignee_phone'] = isset ($_POST['consignee_phone']) ? Input :: getVar(trim($_POST['consignee_phone'])) : '';
			$datainfo['consignee_tel'] = isset ($_POST['consignee_tel']) ? Input :: getVar(trim($_POST['consignee_tel'])) : '';
			$datainfo['province'] = isset ($_POST['province']) ? Input :: getVar(trim($_POST['province'])) : '';
			$datainfo['city'] = isset ($_POST['city']) ? Input :: getVar(trim($_POST['city'])) : '';
			$datainfo['county'] = isset ($_POST['zipcode']) ? Input :: getVar(trim($_POST['county'])) : '';
			$datainfo['zipcode'] = isset ($_POST['zipcode']) ? Input :: getVar(trim($_POST['zipcode'])) : '';
			$datainfo['is_self'] = isset ($_POST['is_self']) ? Input :: getVar(trim($_POST['is_self'])) : 1;
			$datainfo['self_name'] = isset ($_POST['self_name']) ? Input :: getVar(trim($_POST['self_name'])) : '';
			$datainfo['self_phone'] = isset ($_POST['self_phone']) ? Input :: getVar(trim($_POST['self_phone'])) : '';

			//商品及费用信息
			
			//取出购物车
			$cartinfo = cookie('mycart');
			if (empty($cartinfo['good'])){
				$this->error('购物车为空！', 1);
			}
				
			$cartinfo = json_decode(stripcslashes($cartinfo),true);
			foreach ($cartinfo['good'] as $agood) {
				$newgoodinfo = array ();
				if (!empty($agood)) {
					$newgoodinfo = array (
						'orderform_id' => '',
						'good_id' => $agood['good_id'],
						'good_code' => $agood['good_code'],
						'good_name' => $agood['good_name'],
						'good_img' => $agood['good_img'],
						'good_attr' => $agood['good_attr'],
						'good_num' => $agood['good_num'],
						'good_price' => $agood['good_price'],
						'total_fee' => $agood['total'],
						'discount' => 0,
						'getpoint' => 0,
						'is_comment' => 0,
					);
					$datainfo['total_fee'] +=  $agood['total'];
					$datainfo['discount_fee'] += 0;
					$datainfo['good'][] = $newgoodinfo;
				}
			}
			if (empty ($datainfo['good'])) {
				redirect(U('Orderform/createok'));
			}
			//商品价格
			$datainfo['total_fee'] = round($datainfo['total_fee'], 2);
			//运费
			$datainfo['freight_fee'] = $freight_fee;
			//折扣价格

			$datainfo['discount_fee'] = floor($datainfo['discount_fee']);
			//$datainfo['discount_fee'] = null;
			//最终价格
			$datainfo['final_fee'] = $datainfo['total_fee'] + $datainfo['freight_fee'] - $datainfo['discount_fee'];

			$datainfo['detail'] = isset ($_POST['detail']) ? Input :: getVar(trim($_POST['detail'])) : '';
			$datainfo['note'] = '';
			$datainfo['progress'] = 1;
			$datainfo['is_comment'] = 0;
			$datainfo['happen'] = time();
			$datainfo['pay_time'] = '';
			$datainfo['delivery_time'] = '';
			$datainfo['cancel_time'] = '';
			$datainfo['turnoff_time'] = '';
			$datainfo['complete_time'] = '';
			$datainfo['divide_id'] = 0;
			$datainfo['is_divide'] = 0;
			$datainfo['created'] = time();
			$datainfo['status'] = 1;
			$datainfo['grade'] = 0;
			$datainfo['oid'] = 0;
			
			$morder = D('Orderform');
			$morder->startTrans();
			if ($morder->relation('good')->add($datainfo)) {
				//echo $morder->getLastSql();
				//清空购物车
				cookie('mycart',null);
				$morder->commit();
				Session :: set('ordercode', $datainfo['code']);
				Session :: set('orderfee', $datainfo['final_fee']);
				//处理提成
				$rate = $this->getConfig('divide_rate');
				//echo $rate;
				if($rate){
					$md = D('Divide');
					$md->doNew($datainfo,$rate);
				}
				redirect(U('Orderform/createok'));
			} else {
				$morder->rollback();
				$this->error($morder->getError());
			}
		}
	}
	public function createok() {
		$morder = M('Orderform');
		$map['code'] = Session :: get('ordercode');
		$datainfo = $morder->where($map)->find();
		if ($datainfo) {
			$this->assign('datainfo', $datainfo);
			$this->assign('isorder', 4);
			$this->assign('isindex', 1);
			$this->display();
		} else {
			$this->error('你请求的页面没有找到！');
		}

	}
	public function chPayType(){
		$orderform_id = isset($_POST['orderform_id'])?$_POST['orderform_id']:0;
		$paytype = isset($_POST['pay_type'])?$_POST['pay_type']:0;
		if($orderform_id && $paytype){
			$morder = M('Orderform');
			$map['orderform_id'] = $orderform_id;
			$morder->setField('pay_type',$paytype,$map);
			$this->success('修改成功',1);
		}else{
			$this->error('参数错误',1);
		}
	}
	public function getOrderformCode() {
		$timestr = date('ymd') . substr(time() + rand(1, 100), -4);
		$morder = M('Orderform');
		$omap['code'] = $timestr;
		if ($morder->where($omap)->count()) {
			$this->getOrderformCode();
		} else {
			return $timestr;
		}
	}
	
	
	public function query(){
		if($this->isPost()){
			import("ORG.Util.Input");
			$code = isset($_POST['code'])? Input :: getVar(trim($_POST['code'])) : 0;
			$consignee = isset($_POST['consignee'])? Input :: getVar(trim($_POST['consignee'])) : 0;
			$consignee_phone = isset($_POST['consignee_phone'])? Input :: getVar(trim($_POST['consignee_phone'])) :0;
			$map = array();
			$map['q'] = array();
			$map['p'] = -1;
			if($code) $map['q']['code'] = $code;
			if($consignee) $map['q']['consignee'] = $consignee;
			if($consignee_phone) $map['q']['consignee_phone'] = $consignee_phone;
			$rs = $this->obj->doGet($map);
			$data = isset($rs['list'])? $rs['list']:0;
			if(!$data){
				$this->display('nors');
			}else{
				$this->assign('datalist',$data);
				$this->display('rs');
			}
		}else{
			$this->display('query');
		}
	}
}
?>